package cn.iocoder.yudao.module.system.controller.admin.worksaleoutbound;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.worksaleoutbound.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.worksaleoutbound.WorkSaleOutboundDO;
import cn.iocoder.yudao.module.system.service.worksaleoutbound.WorkSaleOutboundService;

@Tag(name = "管理后台 - 销售出库")
@RestController
@RequestMapping("/system/work-sale-outbound")
@Validated
public class WorkSaleOutboundController {

    @Resource
    private WorkSaleOutboundService workSaleOutboundService;

    @PostMapping("/create")
    @Operation(summary = "创建销售出库")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:create')")
    public CommonResult<String> createWorkSaleOutbound(@Valid @RequestBody List<WorkSaleOutboundSaveReqVO> createReqVO) {
        return success(workSaleOutboundService.createWorkSaleOutbound(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售出库")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:update')")
    public CommonResult<Boolean> updateWorkSaleOutbound(@Valid @RequestBody WorkSaleOutboundSaveReqVO updateReqVO) {
        workSaleOutboundService.updateWorkSaleOutbound(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售出库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:delete')")
    public CommonResult<Boolean> deleteWorkSaleOutbound(@RequestParam("id") Integer id) {
        workSaleOutboundService.deleteWorkSaleOutbound(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除销售出库")
                @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:delete')")
    public CommonResult<Boolean> deleteWorkSaleOutboundList(@RequestParam("ids") List<Integer> ids) {
        workSaleOutboundService.deleteWorkSaleOutboundListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售出库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:query')")
    public CommonResult<WorkSaleOutboundRespVO> getWorkSaleOutbound(@RequestParam("id") Integer id) {
        WorkSaleOutboundDO workSaleOutbound = workSaleOutboundService.getWorkSaleOutbound(id);
        return success(BeanUtils.toBean(workSaleOutbound, WorkSaleOutboundRespVO.class));
    }

    @GetMapping("/getODo")
    @Operation(summary = "获得销售出库单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:query')")
    public CommonResult<WorkSaleODORespVO> getODo(@RequestParam("batchNumber") String batchNumber) {
        WorkSaleODORespVO data = workSaleOutboundService.getODo(batchNumber);
        return success(data);
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售出库分页")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:query')")
    public CommonResult<PageResult<WorkSaleOutboundRespVO>> getWorkSaleOutboundPage(@Valid WorkSaleOutboundPageReqVO pageReqVO) {
        PageResult<WorkSaleOutboundRespVO> pageResult = workSaleOutboundService.getWorkSaleOutboundPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售出库 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-sale-outbound:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkSaleOutboundExcel(@Valid WorkSaleOutboundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkSaleOutboundRespVO> list = workSaleOutboundService.getWorkSaleOutboundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售出库.xls", "数据", WorkSaleOutboundRespVO.class,
                        BeanUtils.toBean(list, WorkSaleOutboundRespVO.class));
    }

}
